﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SilverRaport.Web
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SilverRaporService" in code, svc and config file together.
    public class SilverRaporService : ISilverRaporService
    {

        public int RaporKaydet(string RaporIsim, string RaporSQL, int Interval, int KullaniciID, byte raporTipi, byte grafikTipi, int RaporID = 0)
        {
            using (SilverRaporEntities MyEntities = new SilverRaporEntities())
            {
                try
                {
                    if (RaporID != 0)
                    {
                        Raporlar UpdateRapor = MyEntities.Raporlar.FirstOrDefault(x => x.ID == RaporID);
                        UpdateRapor.RaporIsim = RaporIsim;
                        UpdateRapor.RaporSQL = RaporSQL;
                        UpdateRapor.RaporZaman = Interval;
                        UpdateRapor.GrafikTipi = grafikTipi;
                        UpdateRapor.RaporTipi = raporTipi;
                        MyEntities.SaveChanges();
                    }
                    else if (RaporID == 0)
                    {
                        Raporlar Ekle = new Raporlar();
                        Ekle.KullaniciID = KullaniciID;
                        Ekle.RaporIsim = RaporIsim;
                        Ekle.RaporSQL = RaporSQL;
                        Ekle.RaporZaman = Interval;
                        Ekle.RaporTipi = raporTipi;
                        Ekle.GrafikTipi = grafikTipi;

                        MyEntities.Raporlar.AddObject(Ekle);
                        MyEntities.SaveChanges();
                    }
                    using (SilverRaporEntities MyEntities1 = new SilverRaporEntities())
                    {
                        if (RaporID == 0)
                        {
                            Kullanicilar update = MyEntities1.Kullanicilar.FirstOrDefault(x => x.ID == KullaniciID);
                            if (update.SQLKullanilan > 0)
                            {
                                update.SQLKullanilan = update.SQLKullanilan - 1;
                                MyEntities1.SaveChanges();
                            }
                            else
                                return 1;
                        }
                    }
                    return 2;
                }
                catch (Exception)
                {
                    return 3;
                }

            }
        }

        public object KullaniciBul(string UserName, string Pass)
        {
            using (SilverRaporEntities MyEntities = new SilverRaporEntities())
            {
                try
                {
                    var query = MyEntities.Kullanicilar.FirstOrDefault(x => x.Adi.ToLower() == UserName.ToLower() && x.Sifre == Pass);
                    if (query != null)
                        return query.ID;

                    return false;
                }
                catch (Exception)
                {
                    return false;
                }

            }


        }

        public string[] KullaniciGetir(int KullaniciID)
        {
            using (SilverRaporEntities MyEntities = new SilverRaporEntities())
            {
                try
                {
                    var query = MyEntities.Kullanicilar.FirstOrDefault(x => x.ID == KullaniciID);
                    if (query != null)
                        return new string[] { query.Adi, query.Soyadi, query.SQLKullanilan.ToString() };
                    return new string[] { "Boş" };
                }
                catch (Exception ex)
                {
                    return new string[] { "false", ex.Message };
                }


            }
        }

        public List<RaporListe> RaporListele(int KullaniciID)
        {
            using (SilverRaporEntities MyEntities = new SilverRaporEntities())
            {
                var query = from a in MyEntities.Raporlar
                            where a.KullaniciID == KullaniciID
                            select new RaporListe { RaporID = a.ID, RaporInterval = (int)a.RaporZaman, RaporIsim = a.RaporIsim, RaporSQL = a.RaporSQL, GrafikTipi = (byte)a.GrafikTipi, RaporTipi = (byte)a.RaporTipi };

                return query.ToList();
            }
        }

        public bool RaporSil(int RaporID, int KullaniciID)
        {
            using (SilverRaporEntities MyEntities = new SilverRaporEntities())
            {
                try
                {
                    Raporlar delete = MyEntities.Raporlar.FirstOrDefault(x => x.ID == RaporID);
                    MyEntities.DeleteObject(delete);
                    MyEntities.SaveChanges();

                    using (SilverRaporEntities MyEntities1 = new SilverRaporEntities())
                    {
                        Kullanicilar update = MyEntities1.Kullanicilar.FirstOrDefault(x => x.ID == KullaniciID);
                        update.SQLKullanilan += 1;
                        MyEntities1.SaveChanges();
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

    }
}
